package com.jlkf.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jlkf.service.CommonSortService;
import com.platform.server.response.response.pojo.ReturnData;

@Controller
@RequestMapping("/commonSort")
public class CommonSortController {
	
	@Autowired
	private CommonSortService commonSortService;
	
	/**
	 * 查看普通课程分类列表
	 */
	@ResponseBody
	@RequestMapping("/selectCommonSortList")
	public Object selectCommonSortList(Integer id,String type,String G_NAME,String G_RANK,Integer pageNumber,Integer pageSize){
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("G_NAME", G_NAME);
		map.put("G_RANK", G_RANK);
		if(id != null){
			map.put("id", id);
			ModelAndView modelAndView = new ModelAndView();
			List<Map<String, Object>> lists = commonSortService.selectCommonSortList(map);
			modelAndView.addObject("result", lists.get(0));
			if("1".equals(type)){		//新增子类
				modelAndView.setViewName("jsp/commonVideoManager/addSubclass");
			}else if("2".equals(type) && (Integer)lists.get(0).get("G_RANK") == 1){		//编辑
				modelAndView.setViewName("jsp/commonVideoManager/updateOneSort");
			}else{
				modelAndView.setViewName("jsp/commonVideoManager/updateSort");
			}
			return modelAndView;
		}
		PageHelper.startPage(pageNumber,pageSize);
		List<Map<String, Object>> lists = commonSortService.selectCommonSortList(map);
		PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(lists);
		return new ReturnData(lists,pageInfo.getPages(),(int)pageInfo.getTotal(),200,"分类列表");
	}
	
	/**
	 * 删除分类
	 */
	@ResponseBody
	@RequestMapping("/deleteSort")
	public Object deleteSort(String id){
		int flag = commonSortService.deleteSort(id);
		if(flag > 0){
			return new ReturnData(200,"删除成功");
		}else{
			return new ReturnData(500,"删除失败");
		}
	}
	
	/**
	 * 添加一级分类
	 */
	@ResponseBody
	@RequestMapping("/insertOneSort")
	public Object insertOneSort(String G_NAME,String G_IMG){
		int flag = commonSortService.insertOneSort(G_NAME,G_IMG);
		if(flag > 0){
			return new ReturnData(200,"添加成功");
		}else{
			return new ReturnData(500,"删除失败");
		}
	}
	
	/**
	 * 编辑分类
	 */
	@ResponseBody
	@RequestMapping("/updateSort")
	public Object updateSort(Integer G_ID,String G_NAME,String G_IMG){
		Map<String,Object>  map = new HashMap<>();
		map.put("G_ID", G_ID);
		map.put("G_NAME", G_NAME);
		map.put("G_IMG", G_IMG);
		int flag = commonSortService.updateSort(map);
		if(flag > 0){
			return new ReturnData(200,"编辑成功");
		}else{
			return new ReturnData(500,"编辑失败");
		}
	}
	
	/**
	 * 添加子分类
	 */
	@ResponseBody
	@RequestMapping("/insertSubclass")
	public Object insertSubclass(Integer G_ID,String G_NAME){
		ModelAndView modelAndView = (ModelAndView) selectCommonSortList(G_ID,null,null,null,null,null);
		Map<String, Object> model = (Map<String, Object>) modelAndView.getModel().get("result");
		Map<String,Object>  map = new HashMap<>();
		if("1".equals(model.get("G_RANK").toString())){
			map.put("G_RANK", 2);
		}
		if("2".equals(model.get("G_RANK").toString())){
			map.put("G_RANK", 3);
		}
		map.put("G_SUPER", model.get("G_ID"));
		map.put("G_NAME", G_NAME);
		int flag = commonSortService.insertSubclass(map);
		if(flag > 0){
			return new ReturnData(200,"添加成功");
		}else{
			return new ReturnData(500,"添加失败");
		}
	}
}
